package com.suncp.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author suncp
 * @date 2020/12/12 18:49
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        // 放行: 判断什么情况下登录
        // 登录页面也会放行
        if (request.getRequestURI().contains("toLogin")) {
            return true;
        }

        //说明在提交登录
        if (request.getRequestURI().contains("login")) {
            return true;
        }

        // 第一次登录也是没有session的
        if (session.getAttribute("userLoginInfo") != null) {
            return true;
        }

        // 列出什么情况下妹遇登录
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
        return false;
    }
}
